package NK_SEL.applogic3;

//import java.util.NoSuchElementException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import NK_SEL.pages.PageManager;

public class DriverBasedHelper {
  protected Integer nTimes=20;
  protected WebDriver driver;
  protected WebDriverWait wait;
  protected PageManager pages;

  public DriverBasedHelper(WebDriver driver) {
    this.driver = driver;
    wait = new WebDriverWait(driver, 10);
    pages = new PageManager(driver);
  }

  public boolean isTextPresent(String textToBeVerified)  {
	  for (int count=0;; count++){
		 if (count >= nTimes) return false; 
		  try {// System.out.println("in try");
 if (driver.findElement(By.xpath("//*[contains(.,'" + textToBeVerified + "')]")) != null)  {
			    return true;  }
		  }		catch (Exception e) {}
	   }
	  }
  
  public boolean isElementPresentByCSS(String by) {
	    for (int count=0;; count++){
   	     if (count >= nTimes) return false; 
	      try { driver.findElement(By.cssSelector(by)); 
	       return true; } catch (Exception e) {}
	      }
  }
  
  public String closeAlertAndGetItsText(boolean acceptNextAlert) {
	    try {
	      Alert alert = driver.switchTo().alert();
	      String alertText = alert.getText();
	      if (acceptNextAlert) {
	        alert.accept();
	      } else {
	        alert.dismiss();
	      }
	      return alertText;
	    } finally {
	      acceptNextAlert = true;
	    }
  }

}
